package com.wisdomparking.control;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.wisdomparking.bean.Manager;
import com.wisdomparking.config.result.Message;
import com.wisdomparking.config.result.TempResult;
import com.wisdomparking.constant.Constants;
import com.wisdomparking.service.ManagerService;
import com.wisdomparking.service.OrdersService;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.UserService;
import com.wisdomparking.tools.MD5;
import com.wisdomparking.tools.ToolValid;

/**
 * 首页
 */
@RestController
public class IndexController extends BaseController{
	
	@Value("${app.name}")
	public String appName;
	
	@Autowired UserService userService;
	@Autowired OrdersService ordersService;
	@Autowired ParkingService parkingService;
	@Autowired ManagerService managerService;
	
	@Autowired DefaultKaptcha defaultKaptcha; 
	
	/**
	 * 开发中首页
	 */
	@RequestMapping("/")
	public ModelAndView deving(){
		return new ModelAndView("deving");
	}
	
	/**
	 * 首页
	 */
	@RequestMapping("/index")
	public ModelAndView index(){
		
		return new ModelAndView("index");
	}
	
	/**
	 * 管理员注册
	 * 
	 * @param name
	 * @return
	 * @说明： 【只允许车场总管理注册】
	 */
	@RequestMapping("/managerRegister")
	public TempResult managerRegister(Manager manager){
		
		Boolean flag = false;
		if(ToolValid.isNotNull(manager) && ToolValid.isNotEmpty(manager.getUserName())){
			Manager m = managerService.getManagerByUserName(manager.getUserName());
			if(ToolValid.isNull(m)){
				manager.setPassword(MD5.getMD5Str(manager.getPassword()));
				manager.setCreateTime(new Date());
				manager.setManagerGrade(1);
				
				flag = managerService.addManager(manager, true);
			}
		}
		return new TempResult(flag, "");
	}
	
	
	/**
	 * 管理员登录
	 * 
	 * @param userName
	 * @param password
	 * @return
	 */
	@RequestMapping("/managerLogin")
	public Message managerLogin(String userName, String password){
		Boolean flag = false;
		Manager manager = managerService.getManagerByUserName(userName);
		
		if(ToolValid.isNotNull(manager)){
			if(manager.getPassword().equals(MD5.getMD5Str(password))){
				flag = true;
				request.getSession().setAttribute(Constants.LOGININFO, manager);
				return new Message(manager);
			}
		}
		
		return new Message(flag);
	}
	
	
	
	/**
	 * 管理登录页
	 * 
	 * @return
	 */
	@RequestMapping("/login")
	public ModelAndView login(){
		return new ModelAndView("login");
	}
	
	@RequestMapping("/defaultKaptcha")  
	public void defaultKaptcha() throws Exception {
		byte[] captchaChallengeAsJpeg = null;
		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
		try {
			// 生产验证码字符串并保存到session中
			String createText = defaultKaptcha.createText();
			request.getSession().setAttribute("vrifyCode", createText);
			// 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
			BufferedImage challenge = defaultKaptcha.createImage(createText);
			ImageIO.write(challenge, "jpg", jpegOutputStream);
		} catch (IllegalArgumentException e) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}

		// 定义response输出类型为image/jpeg类型，使用response输出流输出图片的byte数组
		captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		ServletOutputStream responseOutputStream = response.getOutputStream();
		responseOutputStream.write(captchaChallengeAsJpeg);
		responseOutputStream.flush();
		responseOutputStream.close();
	}

}
